﻿namespace = grand_ambitions

#######################################
###
###	ONGOING STORY CYCLE EVENTS
###
### 0001-0099 	Story Entry Points
###	0100-0149	Claimant Faction Route
###	0150-0199	Depose Scheme Route
### 0200-0290	Conclusion
### 0300-0390	Follow-Ups
### 0500-0599 	Maintenance
###
#######################################

#######################################
###
###	ENTRY POINTS TO THE STORY
###
###	0001	Powerful Family Coup
###	0002	Claimant Faction Wins
### 0003	Prev Emperor Becomes LAAMP
### 0004 	LAAMP Creation (Hidden)
###
#######################################

scripted_effect create_grand_ambitions_story = {
	# VARIABLE OPTIONS
	# method = coup / scheme
	create_story = {
		type = grand_ambitions_story_cycle
		save_scope_as = new_story
	}
	random_owned_story = {
		limit = { story_type = grand_ambitions_story_cycle }
		save_scope_as = grand_ambitions_story
		set_variable = {
			name = method
			value = flag:$METHOD$
		}
		set_variable = {
			name = target_char
			value = root.top_liege
		}
		set_variable = {
			name = target_title
			value = var:target_char.primary_title
		}
	}

	# Create or increment global variable to track how many of these are going on at once
	if = {
		limit = { exists = global_var:current_grand_ambitions_counter }
		change_global_variable = {
			name = current_grand_ambitions_counter
			add = 1
		}
	}
	else = {
		set_global_variable = {
			name = current_grand_ambitions_counter
			value = 1
		}
	}
}

# You're the head of a powerful family and you have a chance to stage a coup
grand_ambitions.0001 = {
	type = character_event
	title = grand_ambitions.0001.t
	desc = grand_ambitions.0001.desc
	theme = administrative
	override_background = {
		reference = ep3_mediterranean_estate
	}

	left_portrait = {
		character = root
		animation = thinking
	}
	lower_right_portrait = root.top_liege

	cooldown = { years = 20 }
	
	trigger = {
		# Keep the number of these down to a reasonable level
		trigger_if = {
			limit = {
				is_ai = yes
				exists = global_var:current_grand_ambitions_counter
			}
			global_var:current_grand_ambitions_counter <= 2
		}
		# is the house head of a powerful family
		house ?= {
			house_head ?= root
			OR = {
				is_powerful_family = yes
				is_dominant_family = yes
			}
		}
		influence_level >= 3
		is_independent_ruler = no
		highest_held_title_tier >= tier_duchy
		government_allows = administrative
		top_liege ?= {
			primary_title.tier = tier_empire
			government_allows = administrative
			NOR = {
				legitimacy_level >= 4
				influence_level >= 4
			}
		}
		NOT = { any_owned_story = { story_type = grand_ambitions_story_cycle } }
		NOR = {
			has_trait = humble
			has_trait = content
			has_trait = loyal
		}
		years_from_game_start >= 1
		#Should be able to actually afford one of the options
		OR = {
			AND = {
				gold >= massive_gold_value
				influence >= medium_influence_value
			}
			prestige >= major_prestige_value
		}
	}

	weight_multiplier = {
		base = 0
		modifier = {
			add = influence_level
		}
	}

	immediate = {
		top_liege = {
			save_scope_as = target_char
			primary_title = {
				save_scope_as = target_title
			}
		}
	}

	option = { # Start a claimant faction
		name = grand_ambitions.0001.a
		flavor = grand_ambitions.0001.a.flavor

		create_grand_ambitions_story = { METHOD = coup }
		if = {
			limit = { NOT = { has_claim_on = top_liege.primary_title } }
			add_unpressed_claim = top_liege.primary_title
		}
		# Add a secret "staging a coup"
		give_coup_plotter_secret_effect = { TARGET = root.top_liege }

		add_prestige = major_prestige_loss

		stress_impact = {
			brave = medium_stress_impact_loss
			honest = minor_stress_impact_loss
			deceitful = minor_stress_impact_gain
		}

		ai_chance = {
			base = 50
			modifier = {
				add = {
					value = martial
					multiply = 2
				}
			}
			modifier = {
				prestige < major_prestige_value
				factor = 0
			}
		}
	}

	option = { # Start a Depose scheme
		name = grand_ambitions.0001.b
		flavor = grand_ambitions.0001.b.flavor

		create_grand_ambitions_story = { METHOD = scheme }
		# Start Depose Scheme w/ nice modifier
		send_interface_toast = {
			type = event_toast_effect_neutral
			title = grand_ambitions.0001.b.toast
			left_icon = root.top_liege
			right_icon = root.top_liege.primary_title

			custom_tooltip = {
				text = grand_ambitions_start_depose_scheme_tt
				# start le scheme
				## Balanced configuration.
				begin_scheme_with_agents_effect = {
					SCHEME_TYPE = depose
					TARGET_TYPE = target_character
					TARGET_SCOPE = root.top_liege
					# Success.
					AGENT_1 = agent_diplomat
					AGENT_2 = agent_tumbler
					# Speed.
					AGENT_3 = agent_infiltrator
					AGENT_4 = agent_socialite
					# Secrecy.
					AGENT_5 = agent_decoy
				}
				# Add a bussin scheme modifier
				random_scheme = {
					limit = {
						scheme_type = depose
						scheme_target_character = root.top_liege
					}
					add_scheme_modifier = {
						type = scheme_grand_ambitions_modifier
					}
				}
			}
		}

		remove_short_term_gold = massive_gold_value
		change_influence = medium_influence_loss

		stress_impact = {
			craven = minor_stress_impact_loss
			ambitious = medium_stress_impact_loss
			deceitful = medium_stress_impact_loss
			honest = minor_stress_impact_gain
		}

		ai_chance = {
			base = 50
			modifier = {
				add = {
					value = intrigue
					multiply = 2
				}
			}
			modifier = {
				OR = {
					influence < medium_influence_value
					short_term_Gold < massive_gold_value
				}
				factor = 0
			}
		}
	}
	
	option = { # I'm happy just how I am (lame)
		name = grand_ambitions.0001.c

		stress_impact = {
			ambitious = medium_stress_impact_gain
			craven = minor_stress_impact_loss
		}

		ai_chance = {
			base = 30
		}
	}
}

# You took over an Admin Empire, decide what to do with the prev emperor
# Fired from on_war_won_attacker
grand_ambitions.0002 = {
	type = character_event
	title = grand_ambitions.0202.t
	desc = grand_ambitions.0202.desc
	theme = administrative
	override_background = {
		trigger = { scope:defender.capital_county = title:c_byzantion }
		reference = ep3_constantinople_riot
	}
	
	left_portrait = {
		character = root
		animation = schadenfreude
	}
	right_portrait = {
		character = scope:defender
		animation = beg
		override_imprisonment_visuals = yes
	}

	immediate = {
		# Saving these scopes for the follow-up (grand_ambitions.0003) for the old emp which references the lost title
		save_scope_as = actor
		save_scope_as = root_scope # for loc
		primary_title = { save_scope_as = revoked }
		scope:defender = {
			save_scope_as = recipient
			save_scope_as = old_emp
			add_character_flag = {
				flag = no_hat
				days = 5
			}
		}

		if = {
			limit = {
				any_owned_story = {
					story_type = grand_ambitions_story_cycle
					has_variable = method
					var:method = flag:coup
				}
			}
		}
		random_owned_story = {
			limit = {
				story_type = grand_ambitions_story_cycle
				has_variable = method
				var:method = flag:coup
			}
			save_scope_as = story
		}
	}

	option = { # Do not banish or de-nose the loser
		name = grand_ambitions.0202.a
		flavor = grand_ambitions.0202.a.flavor
		add_dread = minor_dread_loss
		change_influence = minor_influence_gain
		every_vassal = {
			limit = { has_vassal_stance = glory_hound }
			custom = every_glory_hound_vassal
			add_opinion = {
				target = root
				modifier = weak_opinion
				opinion = -10
			}
		}
		every_vassal = {
			limit = { has_vassal_stance = courtly }
			custom = every_courtly_vassal
			add_opinion = {
				target = root
				modifier = pleased_opinion
				opinion = 15
			}
		}
		if = {
			limit = {
				has_any_nickname = no
			}
			random = {
				chance = 10
				give_nickname = nick_the_merciful
			}
		}
		scope:defender = {
			add_opinion = {
				target = root
				modifier = mercy_opinion
				opinion = 30
			}
			release_from_prison = yes
		}

		ai_chance = {
			base = 30
			modifier = {
				OR = {
					has_trait = patient
					has_trait = forgiving
					has_trait = humble
				}
				add = 50
			}
			ai_value_modifier = {
				ai_honor = 0.5
				ai_compassion = 0.75
			}
		}
	}

	option = { # Just de-nose the loser
		name = grand_ambitions.0202.b
		flavor = grand_ambitions.0202.b.flavor
		disfigure_recipient_effect = yes
		blind_castrate_and_disfigure_effect = yes #Stress & dread
		torture_blind_castrate_disfigure_opinion_effect = { VERB = disfigured }
		scope:defender = { release_from_prison = yes }

		ai_chance = {
			base = 30
			modifier = {
				OR = {
					has_trait = sadistic
					has_trait = callous
				}
				add = 50
			}
			ai_value_modifier = {
				ai_boldness = 0.5
				ai_greed = 0.25
			}
		}
	}
	
	option = { # De-nose and Banish the scoundrel!
		name = grand_ambitions.0202.c
		flavor = grand_ambitions.0202.c.flavor

		disfigure_recipient_effect = yes
		blind_castrate_and_disfigure_effect = yes #Stress & dread
		torture_blind_castrate_disfigure_opinion_effect = { VERB = disfigured }

		scope:defender = {
			banish_effect = { BANISHER = root }
			# Trigger Succession on the Noble Family Title of the prev emp
			create_title_and_vassal_change = {
				type = revoked
				save_scope_as = change
				add_claim_on_loss = yes
			}
			random_held_title = {
				limit = {
					has_title_law = noble_family_succession_law
				}
				change_title_holder = {
					holder = scope:defender.primary_heir
					change = scope:change
				}
			}
			resolve_title_and_vassal_change = scope:change

			# Make the prev emp become a LAAMP
			show_as_tooltip = {
				create_landless_adventurer_title_effect = {
					REASON = flag:exile
					FLAVOR_CHAR = root
				}
			}
			if = {
				limit = { is_ai = no }
				trigger_event = grand_ambitions.0003
			}
			else = {
				trigger_event = grand_ambitions.0004
			}
			release_from_prison = yes
		}

		ai_chance = {
			base = 50
			modifier = {
				OR = {
					has_trait = vengeful
					has_trait = diligent
					has_trait = wrathful
					has_trait = paranoid
				}
				add = 60
			}
			ai_value_modifier = {
				ai_boldness = 1.5
				ai_zeal = 0.25
			}
		}
	}

	option = {
		name = grand_ambitions.0202.d
		custom_tooltip = grand_ambitions.0202.d.tt

		ai_chance = {
			base = 10
		}
	}

	after = {
		if = {
			limit = {
				scope:story ?= {
					has_variable = hof_support
					var:hof_support = yes
				}
			}
			trigger_event = { # Coronation by the patriarch
				id = grand_ambitions.0201
				days = 2
			}
		}
		if = {
			limit = {
				scope:story ?= { has_variable = promised_foreign_ruler_claim }
			}
			trigger_event = {
				id = grand_ambitions.0300
				days = 5
			}
		}
		if = {
			limit = {
				scope:story ?= {
					NAND = {
						has_variable = hof_support
						has_variable = promised_foreign_ruler_claim
					}
				}
			}
			scope:story = { end_story = yes }
		}
	}
}

# (Player) Prev Emperor Becomes LAAMP
grand_ambitions.0003 = {
	type = character_event
	title = ep3_laamps.0020.t
	desc = ep3_laamps.0020.desc
	theme = administrative
	left_portrait = {
		character = root
		animation = marshal
	}
	lower_left_portrait = scope:alt_1
	lower_center_portrait = scope:alt_2
	lower_right_portrait = scope:alt_3
	override_background = { reference = ep3_city_gate }
	cooldown = { days = 5 }

	immediate = {
		# Create Adventurer
		save_scope_as = adventurer
		hidden_effect = {
			# Plus for our memory variable.
			scope:story.var:target_title = { save_scope_as = lost_primary_title }
			create_landless_adventurer_title_effect = {
				REASON = flag:exile
				FLAVOR_CHAR = scope:actor
			}
		}
		find_playable_relatives_effect = yes
		# Portrait scopes
		if = {
			limit = {
				NOR = {
					exists = scope:alt_3
					scope:alt_1 ?= scope:actor
					scope:alt_2 ?= scope:actor
				}
			}
			scope:actor = { save_scope_as = alt_3 }
		}
	}

	option = {
		name = ep3_laamps.0020.a
		create_landless_adventurer_title_tooltip_effect = yes
		add_internal_flag = special
		scope:actor = {
			trigger_event = {
				id = ep3_laamps.0022
				days = 5
			}
		}
	}

	option = {
		name = ep3_laamps.0001.b
		trigger = { exists = scope:alt_1 }
		laamp_switch_playable_character_effect = { NEW_CHARACTER = scope:alt_1 }
	}
	option = {
		name = ep3_laamps.0001.c
		trigger = { exists = scope:alt_2 }
		laamp_switch_playable_character_effect = { NEW_CHARACTER = scope:alt_2 }
	}
	option = {
		name = ep3_laamps.0001.d
		trigger = {
			scope:alt_3 ?= { playable_relative_trigger = yes }
		}
		laamp_switch_playable_character_effect = { NEW_CHARACTER = scope:alt_3 }
	}
	option = {
		name = ep3_laamps.0001.e
		laamp_game_over_option_effect = yes
	}
}

# LAAMP Creation (Hidden)
grand_ambitions.0004 = {
	hidden = yes
	immediate = {
		# Create Adventurer
		save_scope_as = adventurer
		create_landless_adventurer_title_effect = {
			REASON = flag:exile
			FLAVOR_CHAR = scope:actor
		}
		add_internal_flag = special
	}
}


#######################################
###
###	CLAIMANT FACTION ROUTE
###
### 0100	A Bold Proposition
### 0110	Call to Arms
### 0120	Fortified Foundations
### 0130	Whispers in the Wind
### 0131	Follow-up letter event
### 0140 	A Fellow Governor
### 0170	A Powerful Alliance		
### 0180	A Proper Coronation	
###
### Success Events
### 0200 	The Fateful Night
### 0201	The Road to Power
### 0202  	The Fate of the Fallen
###
### Failure Event
### 0250 	End of the Road
###
### Follow-Up Event
### 0300	Reap What You Sow
###
### Invalidation Event
### 0500 	Target Emperor Dies/No longer holds title
###
#######################################

# Find an interlocutor for the following events
scripted_effect find_coup_interlocutor_effect = {
	random_list = {
		1 = {
			trigger = { exists = cp:councillor_chancellor }
			cp:councillor_chancellor = { save_scope_as = interlocutor }
		}
		1 = {
			trigger = { exists = court_position:chief_eunuch_court_position }
			court_position:chief_eunuch_court_position = { save_scope_as = interlocutor }
		}
		1 = {
			trigger = { exists = cp:councillor_steward }
			cp:councillor_steward = { save_scope_as = interlocutor }
		}
	}
	if = {
		limit = { NOT = { exists = scope:interlocutor } }
		ordered_courtier = {
			order_by = intrigue
			save_scope_as = interlocutor
		}
	}
	if = {
		limit = { NOT = { exists = scope:interlocutor } }
		ordered_pool_character = {
			province = root.location
			order_by = intrigue
			save_scope_as = interlocutor
		}
	}
}

# Find a scheme interlocutor for the following events
scripted_effect find_scheme_interlocutor_effect = {
	if = {
		limit = { exists = cp:councillor_spymaster }
		cp:councillor_spymaster = { save_scope_as = interlocutor }
	}
	else_if = {
		limit = { exists = court_position:chief_eunuch_court_position }
		court_position:chief_eunuch_court_position = { save_scope_as = interlocutor }
	}
	if = {
		limit = { NOT = { exists = scope:interlocutor } }
		ordered_courtier = {
			order_by = intrigue
			save_scope_as = interlocutor
		}
	}
	if = {
		limit = { NOT = { exists = scope:interlocutor } }
		ordered_pool_character = {
			province = root.location
			order_by = intrigue
			save_scope_as = interlocutor
		}
	}
}

scripted_effect set_powerful_family_story_variables_effect = {
	scope:story = {
		set_variable = {
			name = powerful_family
			value = $POWERFUL_HOUSE_SCOPE$
		}
		set_variable = {
			name = powerful_family_method
			value = flag:$METHOD_FLAG$
		}
	}
}

scripted_effect send_bethrothal_notifications_and_apply_opinions_effect = {
	hidden_effect = {
		$SPOUSE_1$ = { save_scope_as = secondary_actor }
		$SPOUSE_2$ = { save_scope_as = secondary_recipient }
		$HOST$ = { save_scope_as = actor }
		$PROMISEE$ = { save_scope_as = recipient }

		scope:actor = {
			if = {
				limit = { NOT = { this = scope:recipient } }
				trigger_event = marriage_interaction.0010
			}
			else_if = { #In my own court
				limit = { #Betrothal?
					OR = {
						scope:secondary_actor = { is_adult = no }
						scope:secondary_recipient = { is_adult = no }
						scope:secondary_actor = { has_been_promised_grand_wedding = yes }
						scope:secondary_recipient = { has_been_promised_grand_wedding = yes }
					}
				}
				send_interface_toast = {
					type = event_toast_effect_good
					title = arrange_marriage_interaction_accept_betrothal_toast
					left_icon = scope:secondary_actor
					right_icon = scope:secondary_recipient
					custom_tooltip  = arrange_marriage_interaction_accept_betrothal_toast_desc
				}
			}
			else = {
				send_interface_toast = {
					type = event_toast_effect_good
					title = arrange_marriage_interaction_accept_toast
					left_icon = scope:secondary_actor
					right_icon = scope:secondary_recipient
					custom_tooltip  = arrange_marriage_interaction_accept_toast_desc
				}
			}
		}

		# Apply any relevant opinion penalties for relatives of spouse.
		scope:secondary_actor = {
			if = { # Polygamy
				limit = {
					any_consort = {
						count >= 2
					}
				}
				every_consort = {
					# If the spouse themselves does not believe in polygamy...
					limit = {
						NOR = {
							hp_accepts_polygamy = yes
							hp_accepts_concubinage = yes
						}
					}

					#... then family members who also do not believe in polygamy aren't happy about having a relative forced into polygamous union with you.
					every_close_family_member = {
						if = {
							limit = {
								NOR = {
									hp_accepts_polygamy = yes
									hp_accepts_concubinage = yes
								}
							}
							add_opinion = {
								target = scope:secondary_actor
								modifier = relative_in_blasphemous_union_opinion
							}
						}
					}

					# NOTE: Spouse's own opinions handled in the 'on_marriage' code on_action inside of marriage_concubinage.txt, and are not included here.
				}
			}
		}
		scope:secondary_recipient = {
			if = { #Same-sex relations
				limit = {
					allowed_to_marry_same_sex_trigger = no
					sex_same_as = scope:secondary_actor
				}
				every_close_family_member = {
					if = {
						limit = {
							allowed_to_marry_same_sex_trigger = no
						}
						add_opinion = {
							target = scope:secondary_actor
							modifier = relative_in_blasphemous_union_opinion
						}
					}
				}
			}
		}
		# Courtly vassals like grand weddings
		scope:secondary_actor = {
			if = {
				limit = {
					any_vassal = {
						has_vassal_stance = courtly
					}
				}
				if = {
					limit = {
						has_been_promised_grand_wedding = yes
					}
					every_vassal = {
						limit = {
							has_vassal_stance = courtly
						}
						add_opinion = {
							target = scope:secondary_actor
							modifier = prestigious_wedding_opinion
						}
					}
				}
			}
		}
		scope:secondary_recipient = {
			if = {
				limit = {
					any_vassal = {
						has_vassal_stance = courtly
					}
				}
				if = {
					limit = {
						has_been_promised_grand_wedding = yes
					}
					every_vassal = {
						limit = {
							has_vassal_stance = courtly
						}
						add_opinion = {
							target = scope:secondary_recipient
							modifier = prestigious_wedding_opinion
						}
					}
				}
			}
		}
	}
}

scripted_effect swear_to_join_claimant_faction_effect = {
	save_scope_as = recruit
	custom_tooltip = {
		text = grand_ambitions.0100.powerful_family_joins_faction
		if = {
			limit = {
				scope:target_char = {
					any_targeting_faction = {
						faction_leader = root
						faction_is_type = claimant_faction
						any_faction_member = {
							NOT = { this = scope:recruit }
						}
					}
				}
			}
			scope:target_char = {
				random_targeting_faction = {
					limit = {
						faction_leader = root
						faction_is_type = claimant_faction
						any_faction_member = {
							NOT = { this = scope:recruit }
						}
					}
					save_scope_as = joined_faction
				}
			}
			show_as_tooltip = { join_faction = scope:joined_faction }
			hidden_effect = {
				join_faction_forced = {
					faction = scope:joined_faction
					forced_by = root
					years = 25
				}
			}
		}
		scope:story = {
			add_to_variable_list = {
				name = promised_to_join_faction
				target = scope:powerful_family_head
			}
		}
	}
}

scripted_trigger foreign_ruler_has_any_interest_in_coup_trigger = {
	save_temporary_scope_as = foreign_ruler_temp
	is_at_war = no
	# They must _at least_ not like the current emperor
	opinion = {
		target = scope:target_char
		value <= 0
	}
	NOR = {
		has_relation_friend = scope:target_char
		has_relation_best_friend = scope:target_char
		has_relation_lover = scope:target_char
		has_relation_soulmate = scope:target_char
	}
	OR = {
		# They have a claim on something within the empire that you could help them with
		any_claim = {
			tier = tier_duchy
			any_de_jure_top_liege = {
				this = scope:target_char
			}
		}
		# there is a bordering county
		any_realm_border_county = {
			any_neighboring_county = {
				holder.top_liege = scope:target_char
			}
		}
		# You have a coastal county to give away
		scope:target_char = {
			any_realm_county = {
				any_county_province = {
					is_coastal = yes
				} 
			}
		}
	}
}

# A Bold Proposition
# Gain a foreign ally; limited to foreign ai characters
grand_ambitions.0100 = {
	type = character_event
	title = grand_ambitions.0100.t
	desc = {
		desc = grand_ambitions.0100.intro
		desc = grand_ambitions.0100.claim
		desc = grand_ambitions.0100.conclusion
	}
	theme = administrative

	left_portrait = {
		character = root
		animation = thinking
	}
	right_portrait = {
		character = scope:interlocutor
		animation = chancellor
	}
	lower_center_portrait = scope:foreign_ruler
	
	trigger = {
		NOT = { has_character_flag = had_event_ga_0100 }
		any_neighboring_and_across_water_top_liege_realm_owner = {
			is_ai = yes
			foreign_ruler_has_any_interest_in_coup_trigger = yes
			save_temporary_scope_as = temp_foreign_caller
			trigger_if = {
				limit = { scope:story = { has_variable_list = foreign_callers } }
				NOT = {
					scope:story = {
						any_in_list = {
							variable = foreign_callers
							this = scope:temp_foreign_caller
						}
					}
				}
			}
		}
	}

	immediate = {
		find_coup_interlocutor_effect = yes
		# Find the ruler we wanna nab
		ordered_neighboring_and_across_water_top_liege_realm_owner = {
			limit = {
				is_ai = yes
				foreign_ruler_has_any_interest_in_coup_trigger = yes 
				save_temporary_scope_as = temp_foreign_caller
				trigger_if = {
					limit = { scope:story = { has_variable_list = foreign_callers } }
					NOT = {
						scope:story = {
							any_in_list = {
								variable = foreign_callers
								this = scope:temp_foreign_caller
							}
						}
					}
				}
			}
			order_by = ep3_foreign_ruler_interest_in_coup_value
			save_scope_as = foreign_ruler
		}
		# Determine the demand
		scope:foreign_ruler = {
			save_scope_as = comparator
			if = { # Look for a claim first
				limit = {
					any_claim = {
						tier = tier_duchy
						any_de_jure_top_liege = {
							this = scope:target_char
						}
					}
				}
				# Pick the closest claim to the foreign ruler's capital
				ordered_claim = {
					limit = {
						tier = tier_duchy
						any_de_jure_top_liege = {
							this = scope:target_char
						}
					}
					order_by = ep3_distance_to_comparator_claim
					save_scope_as = foreign_claim
				}
			}
			else_if = { # Then pick a neighboring duchy
				limit = {
					any_realm_border_county = {
						any_neighboring_county = {
							holder.top_liege = scope:target_char
						}
					}
				}
				random_realm_border_county = {
					random_neighboring_county = {
						limit = { holder.top_liege = scope:target_char }
						duchy = { save_scope_as = foreign_claim }
					}
				}
			}
			else = { # If all else fails, just pick a coastal duchy
				scope:target_char = {
					random_realm_county = {
						limit = {
							any_county_province = {
								is_coastal = yes
							}
						}
						duchy = { save_scope_as = foreign_claim }
					}
				}
			}
			scope:story = {
				set_variable = {
					name = foreign_ruler_demand
					value = scope:foreign_claim
				}
			}
		}

		# Find a counter-offer target
		scope:target_char = {
			if = {
				limit = {
					any_realm_border_county = {
						any_neighboring_county = {
							holder.top_liege = scope:foreign_ruler
						}
					}
				}
				ordered_realm_border_county = {
					limit = {
						any_neighboring_county = {
							holder.top_liege = scope:foreign_ruler
						}
					}
					order_by = ep3_distance_to_comparator_capital_county
					save_scope_as = compromise_county
				}
			}
			else = { # Take the closest coastal province
				ordered_realm_county = {
					limit = {
						any_county_province = {
							is_coastal = yes
						}
					}
					order_by = ep3_distance_to_comparator_capital_county
					save_scope_as = compromise_county
				}
			}
		}

		# Log that we talked to this foreign ruler so they don't ask again
		scope:story = {
			add_to_variable_list = {
				name = foreign_callers
				target = scope:foreign_ruler
			}
		}
	}

	option = { # Agree to their offer
		name = grand_ambitions.0100.a
		custom_tooltip = grand_ambitions.0100.a.claim
		if = {
			limit = {
				any_claim = {
					this = scope:foreign_claim
				}
			}
			remove_claim = scope:foreign_claim
		}
		custom_tooltip = grand_ambitions.0100.a.faction_tooltip

		scope:story = {
			set_variable = {
				name = promised_foreign_ruler_claim
				value = scope:foreign_ruler
			}
		}

		add_character_flag = had_event_ga_0100
		
		stress_impact = {
			base = minor_stress_impact_gain
			trusting = minor_stress_impact_loss
		}

		ai_chance = {
			base = 30
			ai_value_modifier = {
				ai_boldness = 0.5
				ai_greed = -0.25
			}
			modifier = {
				OR = {
					has_trait = ambitious
					has_trait = craven
				}
				add = 40
			}
		}
	}

	option = { # Propose a counter-offer
		name = grand_ambitions.0100.b
		duel = {
			skill = diplomacy
			target = scope:foreign_ruler 
			50 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
					min = -49
				}
				desc = grand_ambitions.0100.b.success
				send_interface_toast = {
					type = event_generic_good_text
					title =	grand_ambitions.0100.b.success
					left_icon = scope:foreign_ruler
					custom_tooltip = grand_ambitions.0100.b.success.tt
					if = {
						limit = {
							any_claim = {
								this = scope:foreign_claim
							}
						}
						remove_claim = scope:foreign_claim
					}
					custom_tooltip = grand_ambitions.0100.a.faction_tooltip
				}
				scope:story = {
					set_variable = {
						name = promised_foreign_ruler_claim
						value = scope:foreign_ruler
					}
				}
				add_character_flag = had_event_ga_0100
			}
			50 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3.5
					min = -49
				}
				desc = grand_ambitions.0100.b.failure
				send_interface_toast = {
					type = event_generic_bad_text
					title =	grand_ambitions.0100.b.failure
					left_icon = scope:foreign_ruler

					add_prestige = medium_prestige_gain
				}
			}
		}

		stress_impact = {
			humble = minor_stress_impact_gain
			patient = minor_stress_impact_gain
		}

		ai_chance = {
			base = 40
			ai_value_modifier = {
				ai_boldness = 0.5
				ai_greed = 0.75
			}
			modifier = {
				OR = {
					has_trait = diligent
					has_trait = ambitious
				}
				add = 50
			}
		}
	}
	
	option = { # Deny their offer
		name = grand_ambitions.0100.c

		stress_impact = {
			arrogant = minor_stress_impact_loss
			paranoid = minor_stress_impact_loss
		}

		ai_chance = {
			base = 50
			ai_value_modifier = {
				ai_honor = 0.5
				ai_greed = 1
			}
			modifier = {
				OR = {
					has_trait = stubborn
					has_trait = arrogant
					has_trait = greedy
					has_trait = content
				}
				add = 50
			}
		}
	}
}

# A Call to Arms
# Event to increase your MAA
grand_ambitions.0110 = {
	type = character_event
	title = grand_ambitions.0110.t
	desc = grand_ambitions.0110.desc
	theme = administrative

	left_portrait = {
		character = root
		animation = inspect_weapon
	}
	right_portrait = {
		character = scope:interlocutor
		animation = personality_honorable
	}

	cooldown = { years = 10 }

	immediate = {
		find_coup_interlocutor_effect = yes
		ep3_pick_random_maa_regiment_effect = {
			OWNER = root
			INCLUDE_BASIC_MAA = no
			INCLUDE_SIEGE_MAA = no
		}
	}

	option = { # Spawn a free MAA
		name = grand_ambitions.0110.a
		ep3_create_random_maa_regiment_effect = {
			OWNER = root
			SIZE = 4
		}

		ai_chance = {
			base = 50
			modifier = {
				number_of_maa_regiments <= 3
				factor = 3
			}
		}
	}

	option = { # Get a modifier to reduce MAA cost
		name = grand_ambitions.0110.b
		add_character_modifier = {
			modifier = ep3_grand_ambitions_cheaper_maa_modifier
			years = 5
		}

		ai_chance = {
			base = 40
		}
	}
	
	option = { # No thanks
		name = grand_ambitions.0110.c
		change_influence = minor_influence_gain

		ai_chance = {
			base = 20
			modifier = {
				influence <= minor_influence_value
				factor = 3
			}
		}
	}
}

# Fortified Foundations
# Event to increase your estate buildings
grand_ambitions.0120 = {
	type = character_event
	title = grand_ambitions.0120.t
	desc = grand_ambitions.0120.desc
	theme = administrative
	override_background = { reference = ep3_mediterranean_estate }

	left_portrait = {
		character = root
		animation = survey
	}
	right_portrait = {
		character = scope:interlocutor
		animation = debating
	}

	cooldown = { years = 10 }

	trigger = {
		has_domicile = yes
		domicile = {
			is_domicile_type = estate
			num_domicile_buildings < 6
		}
		OR = {
			number_maa_regiments_of_base_type:skirmishers > 0
			number_maa_regiments_of_base_type:heavy_infantry > 0
			number_maa_regiments_of_base_type:pikemen > 0
			number_maa_regiments_of_base_type:light_cavalry > 0
			number_maa_regiments_of_base_type:heavy_cavalry > 0
			number_maa_regiments_of_base_type:archers > 0
		}
	}

	immediate = {
		find_coup_interlocutor_effect = yes
		domicile = { save_scope_as = estate }
		# Pick the best building for the MaA type they have the most of
		random_list = {
			0 = { # Barracks for Skirmishers, Heavy Infantry, and Spearmen
				trigger = {
					NOT = {
						domicile = { has_domicile_building = barracks_06 }
					}
				}
				modifier = {
					add = number_maa_regiments_of_base_type:skirmishers
					add = number_maa_regiments_of_base_type:heavy_infantry
					add = number_maa_regiments_of_base_type:pikemen
				}
				save_scope_value_as = {
					name = estate_building
					value = flag:barracks
				}
				random_maa_regiment = {
					limit = {
						OR = {
							is_unit_type = skirmishers
							is_unit_type = heavy_infantry
							is_unit_type = pikemen
						}
					}
					save_scope_as = maa_flavor
				}
			}
			0 = { # Pasteurs for Cavalry
				trigger = {
					NOT = {
						domicile = { has_domicile_building = grazing_land_06 }
					}
				}
				modifier = {
					add = number_maa_regiments_of_base_type:light_cavalry
					add = number_maa_regiments_of_base_type:heavy_cavalry
				}
				save_scope_value_as = {
					name = estate_building
					value = flag:grazing_land
				}
				random_maa_regiment = {
					limit = {
						OR = {
							is_unit_type = light_cavalry
							is_unit_type = heavy_cavalry
						}
					}
					save_scope_as = maa_flavor
				}
			}
			0 = { # Watchtower for Archers
				trigger = {
					NOT = {
						domicile = { has_domicile_building = watchtower_06 }
					}
				}
				modifier = {
					add = number_maa_regiments_of_base_type:archers
				}
				save_scope_value_as = {
					name = estate_building
					value = flag:watchtower
				}
				random_maa_regiment = {
					limit = { is_unit_type = archers }
					save_scope_as = maa_flavor
				}
			}
		}
	}

	option = { # Construct Barracks
		name = grand_ambitions.0120.a
		trigger = {
			scope:estate_building ?= flag:barracks
			number_maa_regiments_of_base_type:pikemen >=1
		}
		# Build or upgrade barracks building
		domicile = {
			if = {
				limit = { has_domicile_building_or_higher = barracks_01 }
				switch = {
					trigger = has_domicile_building
					barracks_01 = { add_domicile_building = barracks_02 }
					barracks_02 = { add_domicile_building = barracks_03 }
					barracks_03 = { add_domicile_building = barracks_04 }
					barracks_04 = { add_domicile_building = barracks_05 }
					barracks_05 = { add_domicile_building = barracks_06 }
				}
			}
			else = {
				add_domicile_building = barracks_01
			}
		}
	}
	
	option = { # Construct Pasteur
		name = grand_ambitions.0120.b
		trigger = { scope:estate_building ?= flag:grazing_land }
		# Build or upgrade pasteurs building
		domicile = {
			if = {
				limit = { has_domicile_building_or_higher = grazing_land_01 }
				switch = {
					trigger = has_domicile_building
					grazing_land_01 = { add_domicile_building = grazing_land_02 }
					grazing_land_02 = { add_domicile_building = grazing_land_03 }
					grazing_land_03 = { add_domicile_building = grazing_land_04 }
					grazing_land_04 = { add_domicile_building = grazing_land_05 }
					grazing_land_05 = { add_domicile_building = grazing_land_06 }
				}
			}
			else = {
				add_domicile_building = grazing_land_01
			}
		}
	}
	
	option = { # Construct Watchtower
		name = grand_ambitions.0120.c
		trigger = { scope:estate_building ?= flag:watchtower }
		# Build or upgrade watchtower building
		domicile = {
			if = {
				limit = { has_domicile_building_or_higher = watchtower_01 }
				switch = {
					trigger = has_domicile_building
					watchtower_01 = { add_domicile_building = watchtower_02 }
					watchtower_02 = { add_domicile_building = watchtower_03 }
					watchtower_03 = { add_domicile_building = watchtower_04 }
					watchtower_04 = { add_domicile_building = watchtower_05 }
					watchtower_05 = { add_domicile_building = watchtower_06 }
				}
			}
			else = {
				add_domicile_building = watchtower_01
			}
		}
	}

	option = { # Fallback Estate Upgrade
		name = grand_ambitions.0120.d
		remove_short_term_gold = medium_gold_value
		add_character_modifier = {
			modifier = ep3_grand_ambitions_local_labor_modifier
			years = 10
		}
	}
}

# Whispers in the Wind
# Event to decrease popular opinion of the emperor
grand_ambitions.0130 = {
	type = character_event
	title = grand_ambitions.0130.t
	desc = {
		desc = grand_ambitions.0130.desc.intro
		first_valid = {
			triggered_desc = {
				trigger = { scope:story.var:target_char.legitimacy_level >= 6 }
				desc = grand_ambitions.0130.desc.legitimacy.6
			}
			triggered_desc = {
				trigger = { scope:story.var:target_char.legitimacy_level >= 5 }
				desc = grand_ambitions.0130.desc.legitimacy.5
			}
			triggered_desc = {
				trigger = { scope:story.var:target_char.legitimacy_level >= 4 }
				desc = grand_ambitions.0130.desc.legitimacy.4
			}
			triggered_desc = {
				trigger = { scope:story.var:target_char.legitimacy_level >= 3 }
				desc = grand_ambitions.0130.desc.legitimacy.3
			}
			triggered_desc = {
				trigger = { scope:story.var:target_char.legitimacy_level >= 2 }
				desc = grand_ambitions.0130.desc.legitimacy.2
			}
			desc = grand_ambitions.0130.desc.legitimacy.1
		}
		desc = grand_ambitions.0130.desc.outro
	}
	theme = administrative
	override_background = { reference = ep3_medi_study }

	left_portrait = {
		character = root
		animation = thinking
	}
	right_portrait = {
		character = scope:interlocutor
		animation = scheme
	}
	lower_center_portrait = scope:story.var:target_char

	cooldown = { years = 10 }

	immediate = {
		find_coup_interlocutor_effect = yes
		if = {
			limit = {
				any_secret = {
					secret_type = secret_coup_plotter
					secret_target = scope:story.var:target_char
				}
			}
			random_secret = {
				limit = {
					secret_type = secret_coup_plotter
					secret_target = scope:story.var:target_char
				}
				save_scope_as = secret
			}
		}
	}

	option = { # Spend Influence
		name = grand_ambitions.0130.a
		show_as_unavailable = { influence <= medium_influence_value }
		change_influence = medium_influence_loss
		scope:story.var:target_char = {
			capital_county = {
				add_county_modifier = {
					modifier = ep3_grand_ambitions_rioting_county_modifier
					years = 10
				}
			}
		}
		random = {
			chance = 15
			# Expose your secret
			scope:secret ?= { reveal_to = scope:story.var:target_char }
			# Show that this gives your liege an imprisonment reason
			show_as_tooltip = {
				reverse_add_opinion = {
					target = scope:target_char
					modifier = coup_plotter_opinion
				}
			}
		}
		add_character_flag = {
			flag = successful_riot
			days = 1
		}

		ai_chance = {
			base = 30
			modifier = {
				influence <= medium_influence_value
				factor = 0
			}
		}
	}

	option = { # Spend Gold
		name = grand_ambitions.0130.c
		show_as_unavailable = { short_term_gold <= major_gold_value }
		remove_short_term_gold = major_gold_value
		scope:story.var:target_char = {
			capital_county = {
				add_county_modifier = {
					modifier = ep3_grand_ambitions_rioting_county_modifier
					years = 10
				}
			}
		}
		random = {
			chance = 30
			# Expose your secret
			scope:secret ?= { reveal_to = scope:story.var:target_char }
			# Show that this gives your liege an imprisonment reason
			show_as_tooltip = {
				reverse_add_opinion = {
					target = scope:target_char
					modifier = coup_plotter_opinion
				}
			}
		}
		add_character_flag = {
			flag = successful_riot
			days = 1
		}

		ai_chance = {
			base = 30
			modifier = {
				short_term_gold <= major_gold_value
				factor = 0
			}
		}
	}

	option = { # Intrigue Challenge
		name = grand_ambitions.0130.b
		change_influence = minor_influence_loss
		duel = {
			skill = intrigue
			value = high_skill_rating
			30 = { # Succeed
				compare_modifier = {
					 value = scope:duel_value
					 multiplier = 3.5
					 min = -29
				}
				desc = grand_ambitions.0130.b.success
				send_interface_toast = {
					type = event_toast_effect_good
					title = grand_ambitions.0130.b.success
					left_icon = scope:story.var:target_char
					right_icon = scope:story.var:target_title
					scope:story.var:target_char = {
						capital_county = {
							add_county_modifier = {
								modifier = ep3_grand_ambitions_rioting_county_modifier
								years = 10
							}
						}
					}
				}
				add_character_flag = {
					flag = successful_riot
					days = 1
				}
			}
			50 = { # Fail
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3.5
					min = -49
				}
				desc = grand_ambitions.0130.b.failure
				send_interface_toast = {
					type = event_toast_effect_bad
					title = grand_ambitions.0130.b.failure
					left_icon = scope:story.var:target_char
					right_icon = scope:story.var:target_title
				}
			}
			20 = { # Crit Fail
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3.5
					min = -19
				}
				desc = grand_ambitions.0130.b.crit_failure
				send_interface_toast = {
					type = event_toast_effect_bad
					title = grand_ambitions.0130.b.crit_failure
					left_icon = scope:story.var:target_char
					right_icon = scope:story.var:target_title
					# Expose your secret
					scope:secret ?= { reveal_to = scope:story.var:target_char }
					# Show that this gives your liege an imprisonment reason
					show_as_tooltip = {
						reverse_add_opinion = {
							target = scope:target_char
							modifier = coup_plotter_opinion
						}
					}
				}
			}
		}

		ai_chance = {
			base = 30
			modifier = {
				add = {
					value = intrigue
					multiply = 2
				}
			}
			modifier = {
				influence <= minor_influence_value
				factor = 0
			}
		}
	}

	option = { # Opt-out
		name = grand_ambitions.0130.d

		ai_chance = {
			base = 10
		}
	}

	after = {
		save_scope_as = schemer
		scope:story.var:target_char = {
			if = {
				limit = {
					is_ai = no
					root = { has_character_flag = successful_riot }
				}
				trigger_event = grand_ambitions.0131
			}
		}
	}
}

# Whispers in the Wind Follow-Up
# Mirror Event for the emperor if they are a player
grand_ambitions.0131 = {
	type = letter_event
	opening = grand_ambitions.0131.opening
	desc = {
		desc = grand_ambitions.0131.desc
		triggered_desc = {
			trigger = {
				any_known_secret = {
					scope:secret ?= this
				}
			}
			desc = grand_ambitions.0131.desc_secret
		}
		desc = grand_ambitions.0131.conclusion
	}
	sender = scope:interlocutor

	immediate = {
		find_scheme_interlocutor_effect = yes
		if = { # secret revealed in previous event, but shown here for consistency
			limit = {
				any_known_secret = {
					scope:secret ?= this
				}
			}
			show_as_tooltip = {
				scope:secret ?= { reveal_to = root }
			}
		}
	}

	option = { # Intrigue Option: Kill the rumors
		name = grand_ambitions.0131.a
		trigger = {
			OR = {
				intrigue >= high_skill_rating
				has_trait = schemer
			}
		}

		capital_county = {
			remove_county_modifier = ep3_grand_ambitions_rioting_county_modifier
		}

		ai_chance = {
			base = 100
		}
	}

	option = { # Spend Influence to get people on your side
		name = grand_ambitions.0131.b
		trigger = {
			NOR = {
				intrigue >= high_skill_rating
				has_trait = schemer
			}
		}

		change_influence = medium_influence_loss
		capital_county = {
			remove_county_modifier = ep3_grand_ambitions_rioting_county_modifier
			add_county_modifier = {
				modifier = ep3_grand_ambitions_hushed_rumors
				years = 10
			}
		}

		ai_chance = {
			base = 50
			modifier = {
				factor = 0
				influence < medium_influence_value
			}
		}
	}

	option = { # Opt Out
		name = grand_ambitions.0131.c
		trigger = {
			NOR = {
				intrigue >= high_skill_rating
				has_trait = schemer
			}
		}

		add_prestige = minor_prestige_loss

		stress_impact = {
			paranoid = medium_stress_impact_gain
		}

		ai_chance = {
			base = 50
			modifier = {
				factor = 0
				prestige <= minor_prestige_value
			}
		}
	}
}

# A Fellow Governor
# Get a hook on a powerful fellow governor
grand_ambitions.0140 = {
	type = character_event
	title = grand_ambitions.0140.t
	desc = {
		desc = grand_ambitions.0140.desc
		first_valid = {
			triggered_desc = {
				trigger = { exists = scope:story_faction }
				desc = grand_ambitions.0140.faction
			}
			desc = grand_ambitions.0140.no_faction
		}
	}
	theme = administrative
	
	left_portrait = {
		character = root
		animation = inspect_weapon
	}
	right_portrait = {
		character = scope:target_gov
		animation = spymaster
	}

	cooldown = { years = 3 } 

	trigger = {
		scope:target_char = { # Is there another vassal we can get a hook on
			any_powerful_vassal = {
				save_temporary_scope_as = temp_vassal
				NOR = {
					has_vassal_stance = courtly
					root = { has_hook = prev }
					scope:story = {
						trigger_if = {
							limit = { has_variable_list = promised_to_join_faction}
							is_target_in_variable_list = {
								name =  promised_to_join_faction
								target = scope:temp_vassal
							}
						}
						trigger_if = {
							limit = { has_variable_list = governor_callers }
							is_target_in_variable_list = {
								name = governor_callers
								target = scope:temp_vassal
							}
						}
					}
					scope:story_faction ?= {
						any_faction_member = {
							this = scope:temp_vassal
						}
						faction_is_at_war = no
					}
				}
				root = {
					can_add_hook = {
						target = prev
						type = strong_influence_hook
					}
				}
			}
		}
	}

	immediate = {
		find_coup_interlocutor_effect = yes
		scope:target_char = {
			ordered_powerful_vassal = {
				limit = {
					save_temporary_scope_as = temp_vassal
					NOR = {
						has_vassal_stance = courtly
						root = { has_hook = prev }
						scope:story = {
							trigger_if = {
								limit = { has_variable_list = promised_to_join_faction}
								is_target_in_variable_list = {
									name =  promised_to_join_faction
									target = scope:temp_vassal
								}
							}
							trigger_if = {
								limit = { has_variable_list = governor_callers }
								is_target_in_variable_list = {
									name = governor_callers
									target = scope:temp_vassal
								}
							}
						}
						scope:story_faction ?= {
							any_faction_member = {
								this = scope:temp_vassal
							}
							faction_is_at_war = no
						}
					}
					root = {
						can_add_hook = {
							target = prev
							type = strong_influence_hook
						}
					}
				}
				order_by = "opinion(root)"
				save_scope_as = target_gov
			}
		}
	}

	option = { # Accept
		name = grand_ambitions.0140.a
		if = {
			limit = {
				exists = scope:story_faction
			}
			scope:target_gov = {
				show_as_tooltip = { join_faction = scope:story_faction }
				hidden_effect = {
					join_faction_forced = {
						faction = scope:story_faction
						forced_by = root
						years = 25
					}
				}
			}
		}
		else = {
			add_hook = {
				target = scope:target_gov
				type = strong_influence_hook
			}
		}
		scope:story = {
			add_to_variable_list = {
				name =  promised_to_join_faction
				target = scope:target_gov
			}
		}

		ai_chance = {
			base = 90
		}
	}

	option = { # Reject
		name = grand_ambitions.0140.b
		scope:story = {
			add_to_variable_list = {
				name = governor_callers
				target = scope:target_gov
			}
		}

		ai_chance = {
			base = 10
		}
	}
}


#######################################
###
###	SCHEME ROUTE
###
### 0150	An Additional Agent		
### 0151	A Master of Whispers	
### 0155	Calling in Favors		
### 0160	Threads of Subterfuge		
### 0170	A Powerful Alliance		
### 0180	A Proper Coronation
### 0190 	Keep Your Friends Close...
### 0191 	A Fateful Offer	
###
### Success Events
### 0200 	The Fateful Night
### 0201	The Road to Power
### 0202  	The Fate of the Fallen
###
### Failure Event
### 0250 	End of the Road
###
### Invalidation Event
### 0500 	Target Emperor Dies/No longer holds title
###
#######################################

# Chances to gain scheme agents 
grand_ambitions.0150 = {
	type = character_event
	title = grand_ambitions.0150.t
	desc = grand_ambitions.0150.desc
	theme = administrative

	left_portrait = {
		character = root
		animation = scheme
	}
	right_portrait = {
		character = scope:interlocutor
		animation = spymaster
	}

	trigger = {
		scope:story = {
			NOT = { has_variable = had_event_ga_0150 }
		}
	}

	immediate = {
		find_scheme_interlocutor_effect = yes
	}

	option = { # Add an infiltrator (speed agent)
		name = grand_ambitions.0150.a
		scope:depose_scheme_scope = {
			add_agent_slot = agent_infiltrator
		}
	}

	option = { # Add a decoy (secrecy agent)
		name = grand_ambitions.0150.b
		scope:depose_scheme_scope = {
			add_agent_slot = agent_decoy
		}
	}
	
	option = { # Add a socialite (success chance agent)
		name = grand_ambitions.0150.c 
		scope:depose_scheme_scope = {
			add_agent_slot = agent_socialite
		}
	}

	after = {
		scope:story = {
			set_variable = {
				name = had_event_ga_0150
				value = yes
			}
		}
	}
}

scripted_trigger can_steal_eunuch_trigger = {
	scope:target_char = { is_ai = yes }
	can_employ_court_position_type = chief_eunuch_court_position
	NOR = {
		scope:eunuch = { has_trait = loyal }
		employs_court_position = chief_eunuch_court_position
	}
	intrigue >= high_skill_rating
}

# Special eunuch agent slot
grand_ambitions.0151 = {
	type = character_event
	title = grand_ambitions.0151.t
	desc = grand_ambitions.0151.desc
	theme = administrative
	override_background = { reference = relaxing_room }

	left_portrait = {
		character = root
		animation = scheme
	}
	right_portrait = {
		character = scope:interlocutor
		animation = spymaster
	}
	lower_center_portrait = scope:eunuch
	
	trigger = {
		any_scheme = {
			scheme_type = depose
			NOT = {
				any_scheme_agent_slot = {
					is_agent_slot_type = agent_eunuch
				}
			}
		}
		scope:target_char = {
			any_court_position_holder = {
				type = chief_eunuch_court_position
			}
		}
	}

	immediate = {
		find_scheme_interlocutor_effect = yes
		scope:target_char = {
			random_court_position_holder = {
				type = chief_eunuch_court_position
				save_scope_as = eunuch
			}
		}
		hidden_effect = {
			scope:depose_scheme_scope = {
				add_agent_slot = agent_eunuch
			}
			scope:depose_scheme_scope = {
				random_scheme_agent_slot = {
					limit = {
						is_filled = no
						is_agent_slot_type = agent_eunuch
					}
					save_scope_as = eunuch_slot
				}
			}
		}
	}

	option = { # Yes add the eunuch to the scheme
		name = grand_ambitions.0151.a
		change_influence = major_influence_loss
			
		show_as_tooltip = {
			scope:depose_scheme_scope = {
				add_agent_slot = agent_eunuch
			}
		}
		scope:eunuch = {
			force_add_to_agent_slot = {
				agent_slot = scope:eunuch_slot
				years = 3
			}
		}
		scope:story = {
			set_variable = {
				name = eunuch
				value = yes
			}
		}

		ai_chance = {
			base = 100
			modifier = {	
				influence <= major_influence_value
				factor = 0
			}
		}
	}

	option = { # I'd rather have him for myself 
		name = grand_ambitions.0151.b
		trigger = {
			can_steal_eunuch_trigger = yes
		}
		change_influence = massive_influence_loss
		remove_short_term_gold = major_gold_value
		# Steal the eunuch
		court_position_grant_effect = {
			CANDIDATE = scope:eunuch
			POS = chief_eunuch
			EMPLOYER = root
		}
		show_as_tooltip = {
			scope:depose_scheme_scope = {
				add_agent_slot = agent_eunuch
			}
		}
		scope:eunuch = {
			force_add_to_agent_slot = {
				agent_slot = scope:eunuch_slot
				years = 3
			}
		}
		scope:story = {
			set_variable = {
				name = eunuch
				value = yes
			}
		}

		ai_chance = {
			base = 80
			modifier = {	
				influence <= massive_influence_value
				factor = 0
			}
		}
	}
	
	option = { # No thanks
		name = grand_ambitions.0151.c
		hidden_effect = {
			scope:depose_scheme_scope = {
				remove_agent_slot = scope:eunuch_slot
			}
		}

		ai_chance = {
			base = 10
		}
	}
}

# Spymaster offers you 3 choices for an unfilled scheme agent slot
grand_ambitions.0155 = {
	type = character_event
	title = grand_ambitions.0155.t
	desc = grand_ambitions.0155.desc
	theme = administrative

	left_portrait = {
		character = root
		animation = spymaster
	}
	right_portrait = {
		character = scope:interlocutor
		animation = scheme
	}
	lower_left_portrait = scope:agent_choice_2
	lower_center_portrait = scope:agent_choice_1
	lower_right_portrait = scope:agent_choice_3
	
	trigger = {
		# Scheme must have unfilled agent slots
		any_scheme = {
			scheme_type = depose
			any_scheme_agent_slot = {
				is_filled = no
				save_temporary_scope_as = temp_agent_slot
			}
			# The eunuch agent slot gets a bespoke event
			scope:temp_agent_slot = {
				NOT = { is_agent_slot_type = agent_eunuch }
			}
		}
		# target_char must have at least one valid courtier to fill the position
		any_courtier_or_guest = {
			NOT = {
				scope:depose_scheme_scope = {
					scheme_is_character_agent = prev 
				}
			}
			is_valid_as_agent_in_slot = scope:temp_agent_slot
		}
	}

	immediate = {
		find_scheme_interlocutor_effect = yes

		# Pick an empty agent slot
		scope:depose_scheme_scope = {
			random_scheme_agent_slot = {
				limit = {
					is_filled = no
				}
				save_scope_as = agent_slot
			}
		}

		# Find 3 characters with which to fill the agent slot 
		scope:target_char = {
			random_courtier_or_guest = {
				limit = {
					NOT = {
						scope:depose_scheme_scope = {
							scheme_is_character_agent = prev 
						}
					}
					is_valid_as_agent_in_slot = scope:agent_slot
				}
				save_scope_as = agent_choice_1
			}
			if = {
				limit = {
					any_courtier_or_guest = {
						NOR = {
							scope:depose_scheme_scope = {
								scheme_is_character_agent = prev 
							}
							this = scope:agent_choice_1
						}
						is_valid_as_agent_in_slot = scope:agent_slot
					}
				}
				random_courtier_or_guest = {
					limit = {
						NOR = {
							scope:depose_scheme_scope = {
								scheme_is_character_agent = prev 
							}
							this = scope:agent_choice_1
						}
						is_valid_as_agent_in_slot = scope:agent_slot
					}
					save_scope_as = agent_choice_2
				}
			}
			if = {
				limit = {
					any_courtier_or_guest = {
						NOR = {
							scope:depose_scheme_scope = {
								scheme_is_character_agent = prev 
							}
							this = scope:agent_choice_1
							this ?= scope:agent_choice_2
						}
						is_valid_as_agent_in_slot = scope:agent_slot
					}
				}
				random_courtier_or_guest = {
					limit = {
						NOR = {
							scope:depose_scheme_scope = {
								scheme_is_character_agent = prev 
							}
							this = scope:agent_choice_1
							this ?= scope:agent_choice_2
						}
						is_valid_as_agent_in_slot = scope:agent_slot
					}
					save_scope_as = agent_choice_3
				}
			}
		}
	}

	option = { # Agent Choice 1
		name = grand_ambitions.0155.a
		scope:agent_choice_1 = {
			force_add_to_agent_slot = {
				agent_slot = scope:agent_slot
				years = 3
			}
		}
	}

	option = { # Agent Choice 2
		name = grand_ambitions.0155.b
		trigger = { exists = scope:agent_choice_2 }
		scope:agent_choice_2 = {
			force_add_to_agent_slot = {
				agent_slot = scope:agent_slot
				years = 3
			}
		}
	}
	
	option = { # Agent Choice 3
		name = grand_ambitions.0155.c
		trigger = { exists = scope:agent_choice_3 }
		scope:agent_choice_3 = {
			force_add_to_agent_slot = {
				agent_slot = scope:agent_slot
				years = 3
			}
		}
	}

	option = { # None of these
		name = grand_ambitions.0155.d
	}
}

# Chance to gain scheme modifiers
grand_ambitions.0160 = {
	type = character_event
	title = grand_ambitions.0160.t
	desc = grand_ambitions.0160.desc
	theme = administrative

	left_portrait = {
		character = root
		animation = scheme
	}
	right_portrait = {
		character = scope:interlocutor
		animation = spymaster
	}
	lower_center_portrait = scope:target_char

	trigger = {
		scope:story = {
			NOT = { has_variable = had_event_ga_0160 }
		}
	}

	immediate = {
		find_scheme_interlocutor_effect = yes
	}

	option = { # Bribe the palace functionaries
		name = grand_ambitions.0160.a
		change_influence = minor_influence_loss
		random_scheme = {
			limit = { scheme_type = depose }
			add_scheme_modifier = {	
				type = scheme_grand_ambitions_bribed_functionaries_modifier
			}
		}
	}

	option = { # Bribe some merchants
		name = grand_ambitions.0160.b
		remove_short_term_gold = major_gold_value
		random_scheme = {
			limit = { scheme_type = depose }
			add_scheme_modifier = {	
				type = scheme_grand_ambitions_bribed_merchants_modifier
			}
		}
	}
	
	option = { # Bribe the scribes
		name = grand_ambitions.0160.c
		remove_short_term_gold = medium_gold_value
		random_scheme = {
			limit = { scheme_type = depose }
			add_scheme_modifier = {	
				type = scheme_grand_ambitions_bribed_scribes_modifier
			}
		}
	}

	after = {
		scope:story = {
			set_variable = {
				name = had_event_ga_0160
				value = yes
			}
		}
	}
}

# Another Powerful Family could support you
grand_ambitions.0170 = {
	type = character_event
	title = grand_ambitions.0170.t
	desc = grand_ambitions.0170.desc
	theme = administrative
	override_background = { reference = relaxing_room }

	left_portrait = {
		character = root
		animation = scheme
	}
	right_portrait = {
		character = scope:interlocutor
		animation = spymaster
	}
	lower_center_portrait = scope:powerful_family_head
	lower_right_portrait = scope:marriage_proposal

	cooldown = { months = 12 }

	trigger = {
		scope:story = {
			NOT = { has_variable = powerful_family }
		}
		scope:target_char = {
			any_vassal = {
				primary_title.tier >= tier_duchy
				this = house.house_head
				NOT = { this = root }
				house = { is_powerful_family = yes }
			}
		}
	}

	immediate = {
		find_scheme_interlocutor_effect = yes
		scope:target_char = {
			random_vassal = {
				limit = { 
					primary_title.tier >= tier_duchy
					this = house.house_head
					NOT = { this = root }
					is_imprisoned = no
					opinion = { # they can dislike you if it's a beneficial deal
						target = root
						value >= -30
					}
				}
				weight = { # Prefer picking a vassal that has a child ready for marriage and has high military strength
					base = 1
					modifier = {
						add = 100
						any_child = {
							can_marry_character_trigger = {
								CHARACTER = root
							}
						}
					}
					modifier = {
						add = this.current_military_strength
					}
				}
				save_scope_as = powerful_family_head
				house = { save_scope_as = powerful_house }
			}
		}

		save_scope_as = comparator # used in age_different script value below
		if = {
			limit = {
				scope:powerful_family_head = {
					any_child = {
						can_marry_character_trigger = {
							CHARACTER = root
						}
					}
				}
			}
			scope:powerful_family_head = {
				ordered_child = {
					order_by = age_difference
					limit = {
						can_marry_character_trigger = {
							CHARACTER = root
						}
					}
					save_scope_as = marriage_proposal
				}
			}
		}
	}

	option = { # Gold, Influence, and Prestige
		name = grand_ambitions.0170.a
		change_influence = major_influence_loss
		pay_short_term_gold = {
			target = scope:powerful_family_head
			gold = major_gold_value
		}
		add_prestige = massive_prestige_loss

		if = { # Scheme Track
			limit = {
				scope:story.var:method ?= flag:scheme
				exists = scope:depose_scheme_scope
			}
			scope:depose_scheme_scope = {
				add_scheme_modifier = {
					type = scheme_grand_ambitions_powerful_family_modifier
				}
			}
		}
		else = { # Coup Track
			# House head promises to join your faction
			scope:powerful_family_head = {
				swear_to_join_claimant_faction_effect = yes
			}
		}

		set_powerful_family_story_variables_effect = {
			POWERFUL_HOUSE_SCOPE = scope:powerful_house
			METHOD_FLAG = currency
		}

		ai_chance = {
			base = 30
			modifier = {
				OR = {
					prestige <= massive_prestige_value
					short_term_gold <= major_gold_value
					influence <= major_influence_value
				}
				factor = 0
			}
		}
	}

	option = { # Hook
		name = grand_ambitions.0170.b
		scope:powerful_family_head = {
			add_hook = {
				target = root
				type = favor_hook
			}
		}

		if = { # Scheme Track
			limit = {
				scope:story.var:method ?= flag:scheme
				exists = scope:depose_scheme_scope
			}
			scope:depose_scheme_scope = {
				add_scheme_modifier = {
					type = scheme_grand_ambitions_powerful_family_modifier
				}
			}
		}
		else = { # Coup Track
			# House head promises to join your faction
			scope:powerful_family_head = {
				swear_to_join_claimant_faction_effect = yes
			}
		}

		set_powerful_family_story_variables_effect = {
			POWERFUL_HOUSE_SCOPE = scope:powerful_house
			METHOD_FLAG = hook
		}

		stress_impact = {
			paranoid = minor_stress_impact_gain 
			trusting = minor_stress_impact_loss
		}

		ai_chance = {
			base = 30
		}
	}
	
	option = { # Betrothal
		name = grand_ambitions.0170.c
		trigger = {
			trigger_if = {
				limit = {
					NOR = {
						hp_accepts_polygamy = yes
						allowed_more_spouses = yes
					}
				}
				is_married = no
			}
			is_betrothed = no
			exists = scope:marriage_proposal
		}

		if = {
			limit = {
				has_ep2_dlc_trigger = yes
			}
			create_grand_wedding_betrothal = {
				SPOUSE_1 = root
				SPOUSE_2 = scope:marriage_proposal
				HOST = root
				PROMISEE = scope:powerful_family_head
			}
		}
		else = {
			create_betrothal = scope:marriage_proposal
		}

		send_bethrothal_notifications_and_apply_opinions_effect = {
			SPOUSE_1 = root
			SPOUSE_2 = scope:marriage_proposal
			HOST = root
			PROMISEE = scope:powerful_family_head
		}

		if = { # Scheme Track
			limit = {
				scope:story.var:method ?= flag:scheme
				exists = scope:depose_scheme_scope
			}
			scope:depose_scheme_scope = {
				add_scheme_modifier = {
					type = scheme_grand_ambitions_powerful_family_modifier
				}
			}
		}
		else = { # Coup Track
			# House head promises to join your faction
			scope:powerful_family_head = {
				swear_to_join_claimant_faction_effect = yes
			}
		}

		set_powerful_family_story_variables_effect = {
			POWERFUL_HOUSE_SCOPE = scope:powerful_house
			METHOD_FLAG = marriage
		}

		stress_impact = {
			chaste = medium_stress_impact_gain 
		}

		ai_chance = {
			base = 60
		}
	}
	
	option = { # My ambitions will not be shackled by debts
		name = grand_ambitions.0170.d
		change_influence = minor_influence_gain

		ai_chance = {
			base = 10
		}
	}
}

# Secure support from the patriarch to crown you (legitimacy)
grand_ambitions.0180 = {
	type = character_event
	title = grand_ambitions.0180.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:target_char.capital_county = title:e_byzantium }
				desc = grand_ambitions.0180.intro_hagia_sofia
			}
		}
		desc = grand_ambitions.0180.desc
	}
	theme = administrative

	override_background = {
		reference = temple_scope
	}

	left_portrait = {
		character = root
		animation = prayer
	}
	right_portrait = {
		character = root.faith.religious_head
		triggered_animation = {
			trigger = { religion = religion:christianity_religion }
			animation = acknowledging # anim uses crucifix scepter
		}
		animation = war_over_tie
	}

	trigger = {
		any_owned_story = {
			story_type = grand_ambitions_story_cycle
			NOT = { has_variable = hof_support }
		}
	}

	immediate = {
		root.faith.religious_head = {
			save_scope_as = hof
			save_scope_as = background_temple_scope
		}
	}

	option = { 
		name = grand_ambitions.0180.a
		trigger = { piety_level >= max_piety_level }
		flavor = grand_ambitions.0180.a.flavor
		reason = piety_level
		custom_tooltip = grand_ambitions.0180.b.success.tt
		scope:story = {
			set_variable = {
				name = hof_support
				value = yes
			}
		}
	}

	option = { # You have a massive influence level
		name = grand_ambitions.0180.d
		trigger = {
			influence_level >= 4
		}
		flavor = grand_ambitions.0180.d.flavor
		reason = influence_level
		custom_tooltip = grand_ambitions.0180.b.success.tt
		scope:story = {
			set_variable = {
				name = hof_support
				value = yes
			}
		}

		ai_chance = {
			base = 100
		}
	}

	option = { # Convince the Patriarch to crown you
		name = grand_ambitions.0180.b

		change_influence = major_influence_loss
		duel = {
			skills = { learning intrigue }
			value = very_high_skill_rating 
			50 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = 5
					min = -49
				}
				modifier = {	
					num_virtuous_traits >= 1
					add = 10
				}
				desc = grand_ambitions.0180.b.success
				send_interface_toast = {
					type = event_generic_good
					title =	grand_ambitions.0180.b.success
					left_icon = scope:hof
					right_icon = scope:target_title

					custom_tooltip = grand_ambitions.0180.b.success.tt
				}
				if = {
					limit = {
						can_add_hook = {
							target = scope:hof
							type = manipulation_hook
						}
					}
					add_hook = {
						target = scope:hof
						type = manipulation_hook
					}
				}
				scope:story = {
					set_variable = {
						name = hof_support
						value = yes
					}
				}
			}
			50 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = -5
					min = -49
				}
				modifier = {
					num_sinful_traits >= 1
					add = 15
				}
				desc = grand_ambitions.0180.b.failure
				send_interface_toast = {
					type = event_generic_bad
					title =	grand_ambitions.0180.b.failure
					left_icon = scope:hof
					right_icon = scope:target_title

					reverse_add_opinion = {
						target = scope:hof
						modifier = disgusted_opinion
						opinion = -30
					}

					scope:story = {
						set_variable = {
							name = hof_support
							value = no
						}
					}
				}
			}
		}

		ai_chance = {
			base = 50
			modifier = {
				add = intrigue
			}
			modifier = {
				add = learning
			}
		}
	}
	
	option = { # Try to bribe the patriarch
		name = grand_ambitions.0180.c

		scope:hof = {
			random_list = {
				50 = { # Success
					desc = grand_ambitions.0180.b.success
					root = {
						send_interface_toast = {
							type = event_generic_good
							title =	grand_ambitions.0180.b.success
							left_icon = scope:hof
							right_icon = scope:target_title

							custom_tooltip = grand_ambitions.0180.b.success.tt
							remove_short_term_gold = massive_gold_value
						}
					}
					scope:story = {
						set_variable = {
							name = hof_support
							value = yes
						}
					}

					# Traits 
					modifier = {
						has_trait = greedy
						add = 50
					}
					modifier = {
						has_trait = deceitful
						add = 20
					}
					modifier = {
						has_trait = fickle
						add = 15
					}
					modifier = {
						has_trait = arbitrary
						add = 10
					}
					modifier = {
						has_trait = ambitious
						add = 10
					}

					# Relationships
					modifier = {
						has_any_bad_relationship_with_character_trigger = { CHARACTER = scope:target_char }
						add = 40
					}
					opinion_modifier = {
						opinion_target = root
						multiplier = 1.0
					}
					opinion_modifier = {
						opinion_target = root
						multiplier = -1.0
					}
				}
				50 = { # Failure 
					desc = grand_ambitions.0180.b.failure
					root = {
						send_interface_toast = {
							type = event_generic_good
							title =	grand_ambitions.0180.b.success
							left_icon = scope:hof
							right_icon = scope:target_title

							add_piety = medium_piety_loss
							prev = {
								add_opinion = {
									target = root
									modifier = disgusted_opinion
									opinion = -30
								}
							}
						}
					}
					scope:story = {
						set_variable = {
							name = hof_support
							value = no
						}
					}

					# Traits 
					modifier = {
						has_trait = just
						add = 50
					}
					modifier = {
						has_trait = honest
						add = 20
					}
					modifier = {
						has_trait = content
						add = 10
					}

					# Relationships
					modifier = {
						has_any_good_relationship_with_character_trigger = { CHARACTER = scope:target_char }
						add = 40
					}

					# Opinion
					opinion_modifier = {
						opinion_target = root
						multiplier = -1.0
					}
					opinion_modifier = {
						opinion_target = root
						multiplier = 1.0
					}
				}
			}
		}

		ai_chance = {
			base = 50
			ai_value_modifier = {
				ai_greed = -0.5
			}
		}
	}

	option = { # You happen to have a hook
		name = grand_ambitions.0180.e
		trigger = {
			OR = {
				has_strong_hook = scope:hof
				has_weak_hook = scope:hof
			}
		}
		custom_tooltip = grand_ambitions.0180.b.success.tt
		if = {
			limit = {
				has_weak_hook = scope:hof
			}
			remove_hook = {
				target = scope:hof
				type = weak_hook
			}
		}
		else = {
			remove_hook = {
				target = scope:hof
				type = strong_hook
			}
		}
		scope:story = {
			set_variable = {
				name = hof_support
				value = yes
			}
		}
		ai_chance = {
			base = 100
		}
	}

	option = { # Back out of the deal
		name = grand_ambitions.0180.f
		ai_chance = {
			base = 10
		}
	}
}

# You found out a vassal has the Grand Ambitions story cycle, offer them co-emperorship?
grand_ambitions.0190 = {
	type = character_event
	title = grand_ambitions.0190.t
	desc = grand_ambitions.0190.desc
	theme = emperor

	left_portrait = {
		character = root
		animation = anger
	}
	right_portrait = {
		character = scope:interlocutor
		animation = spymaster
	}
	lower_center_portrait = scope:secret_owner

	trigger = {
		# Ensure root is not already the co-emperor
		NAND = {
			has_diarchy_type = co_emperorship
			diarch = scope:secret_owner
		}
		# Ensure the secret owner is viable for co-emperorship
		scope:secret_owner = {
			age >= 16
		}
	}
	
	immediate = {
		save_scope_as = emperor
		if = { # Grab spymaster first
			limit = {
				exists = cp:councillor_spymaster
				cp:councillor_spymaster != scope:secret_owner
			}
			cp:councillor_spymaster = { save_scope_as = interlocutor }
		}
		else_if = { # then chief eunuch
			limit = {
				exists = court_position:chief_eunuch_court_position
				court_position:chief_eunuch_court_position != scope:secret_owner
			}
			court_position:chief_eunuch_court_position = { save_scope_as = interlocutor } 
		}
		else_if = { # then chancellor
			limit = {
				exists = cp:councillor_chancellor
				cp:councillor_chancellor != scope:secret_owner
			}
			cp:councillor_chancellor = { save_scope_as = interlocutor }
		}
		else_if = { # fallback is random family member
			limit = {
				any_close_family_member = {
					this != scope:secret_owner
					age >= 16
				}
			}
			ordered_close_family_member = {
				limit = {
					this != scope:secret_owner
					age >= 16
				}
				order_by = intrigue
				save_scope_as = interlocutor
			}
		}
		else_if = {
			limit = {
				any_courtier = {
					this != scope:secret_owner
					age >= 16
				}
			}
			ordered_courtier = {
				limit = {
					this != scope:secret_owner
					age >= 16
				}
				order_by = intrigue
				save_scope_as = interlocutor
			}
		}
		else = {
			ordered_pool_character = {
				limit = { this != scope:secret_owner }
				province = root.location
				order_by = intrigue
				save_scope_as = interlocutor
			}
		}
	}

	option = { # Offer them Co-Emperorship
		name = grand_ambitions.0190.a
		custom_tooltip = grand_ambitions.0190.a.tt
		random_list = {
			50 = {
				show_chance = no
				desc = co_emperor_accepts
				show_as_tooltip = {
					scope:emperor = {
						custom_tooltip = {
							text = grand_ambitions.0190.a.tt.1
							try_start_diarchy = co_emperorship
							set_diarch = scope:secret_owner
						}
					}
					scope:secret_owner = {
						custom_tooltip = {
							text = grand_ambitions.0190.a.tt.2
							random_secret = {
								limit = { secret_type = secret_coup_plotter }
								remove_secret = yes
							}
							random_owned_story = {
								limit = { story_type = grand_ambitions_story_cycle }
								end_story = yes
							}
						}
						change_influence = major_influence_gain
					}
				}
			}
			50 = {
				show_chance = no
				desc = co_emperor_rejects
				show_as_tooltip = {
					change_influence = major_influence_loss
					scope:secret_owner = { change_influence = major_influence_gain }
				}
			}
		}
		scope:secret_owner = {
			trigger_event = {
				id = grand_ambitions.0191
				days = 5
			}
		}

		ai_chance = {
			base = 50
			modifier = {
				scope:secret_owner = { is_ai = no }
				add = 50
			}
			modifier = {
				OR = {
					has_trait = deceitful
					has_trait = craven
					has_trait = humble
					has_trait = trusting
				}
				add = 100
			}
		}
	}

	option = { # Imprison them
		name = grand_ambitions.0190.b
		add_tyranny = massive_tyranny_gain
		add_dread = major_dread_gain
		imprison = { target = scope:secret_owner type = dungeon }
		scope:secret_owner = {
			add_opinion = {
				target = root
				modifier = imprisoned_me_dungeon
			}
			every_close_family_member = {
				custom = every_family_member_of_secret_owner
				add_opinion = {
					target = root
					modifier = imprisoned_family_member_dungeon
				}
			}
			random = {
				chance = 30
				house_feud_start_effect = {
					# Feuding House Head
					ACTOR = scope:secret_owner
					# Target House Head
					TARGET = scope:emperor
					# Feud Reason
					REASON = head_imprisoned
					# House Member attacker if relevant
					ATTACKER = scope:emperor
					# House Member victim if relevant
					VICTIM = scope:secret_owner
				}
			}
		}

		ai_chance = {
			base = 50
			modifier = {
				scope:secret_owner = { is_ai = no }
				add = -50
			}
			modifier = {
				OR = {
					has_trait = wrathful
					has_trait = arrogant
					has_trait = ambitious
					has_trait = paranoid
				}
				add = 100
			}
		}
	}
	
	option = { # Opt-Out
		name = grand_ambitions.0190.c

		ai_chance = {
			base = 0
		}
	}
}

# Current Emperor offers to make you their Co-Emperor
grand_ambitions.0191 = {
	type = character_event
	title = grand_ambitions.0191.t
	desc = grand_ambitions.0191.desc
	theme = emperor

	left_portrait = {
		character = root
		animation = thinking
	}
	right_portrait = {
		character = scope:interlocutor
		animation = obsequious_bow
	}
	lower_center_portrait = scope:emperor
	
	trigger = {
		age >= 16
		scope:emperor = {
			# Ensure root is not already the co-emperor
			NAND = {
				has_diarchy_type = co_emperorship
				diarch = root
			}
		}
		# The Emperor knows your secret
		any_secret = {
			secret_type = secret_coup_plotter
			secret_target = scope:emperor
			is_known_by = scope:emperor
		}
	}

	immediate = {
		find_scheme_interlocutor_effect = yes
	}

	option = { # Accept
		name = grand_ambitions.0191.a
		scope:emperor = {
			custom_tooltip = {
				text = grand_ambitions.0190.a.tt.1
				try_start_diarchy = co_emperorship
				set_diarch = prev
			}
		}
		scope:secret_owner = {
			random_secret = {
				limit = { secret_type = secret_coup_plotter }
				remove_secret = yes
			}
			random_owned_story = {
				limit = { story_type = grand_ambitions_story_cycle }
				end_story = yes
			}
			change_influence = major_influence_gain
		}
	}

	option = { # Decline
		name = grand_ambitions.0191.b
		scope:emperor = { change_influence = major_influence_loss }
		change_influence = major_influence_gain
	}
}


#######################################
###
###	CONCLUSION EVENTS
###
###	0200	Success
### 0201	Coronation by HoF
### 0202 	Deposed Emperor
###
### 0250 	Failure
###
#######################################

### SUCCESS (0200-0240)
# Congrats, you're emperor now (riffs off event 0002)
grand_ambitions.0200 = {
	type = character_event
	title = grand_ambitions.0200.t
	desc = {
		desc = grand_ambitions.0200.opening
		triggered_desc = {
			trigger = {
				scope:scheme = {
					any_scheme_agent_slot = {
						is_agent_slot_type = agent_eunuch
					}
				}
				scope:story.var:eunuch ?= yes
			}
			desc = grand_ambitions.0200.eunuch
		}
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:story ?= { has_variable = powerful_family_method }
				}
				desc = grand_ambitions.0200.powerful_family
			}
		}
		first_valid = {
			triggered_desc = {
				trigger = { scope:story.var:powerful_family_method ?= flag:currency }
				desc = grand_ambitions.0200.powerful_family.currency
			}
			triggered_desc = {
				trigger = { scope:story.var:powerful_family_method ?= flag:hook }
				desc = grand_ambitions.0200.powerful_family.hook
			}
			triggered_desc = {
				trigger = { scope:story.var:powerful_family_method ?= flag:marriage }
				desc = grand_ambitions.0200.powerful_family.marriage
			}
			desc = grand_ambitions.0200.desc
		}
		desc = grand_ambitions.0200.conclusion
	}
	theme = administrative
	override_background = {
		trigger = { scope:target.capital_county = title:c_byzantion }
		reference = ep3_constantinople_riot
	}

	left_portrait = {
		character = root
		animation = celebrate_dagger
	}
	right_portrait = {
		character = scope:target
		animation = fear
	}

	immediate = {
		if = {
			limit = {
				any_owned_story = {
					story_type = grand_ambitions_story_cycle
					var:target_char ?= scope:target
				}
			}
			random_owned_story = {
				limit = {
					story_type = grand_ambitions_story_cycle
					var:target_char ?= scope:target
				}
				save_scope_as = story
			}
		}
		else_if = {
			limit = {
				any_scheme = {
					scheme_type = depose
				}
			}
			random_scheme = {
				limit = {
					scheme_type = depose
				}
				scheme_target_character = {
					save_scope_as = recipient
					save_scope_as = old_emp
				}
			}
		}
	}

	option = {
		name = grand_ambitions.0200.a
		imprison = {
			target = scope:target
			type = dungeon
		}
		# Root becomes next in line for the empire title
		if = {
			limit = {
				scope:target = {
					has_realm_law_flag = appointment_type_succession
				}
			}
			scope:target.primary_title = {
				current_heir = { save_scope_as = next_heir }
				change_appointment_investment = {
					target = root
					value = {
						value = "scope:next_heir.appointment_candidate_score(scope:target.primary_title)"
						add = 100
					}
				}
			}
			scope:target = { depose = yes }
		}
		else = {
			scope:target = {
				every_vassal = { add_to_list = target_vassals_list }
				add_to_list = target_vassals_list
				every_held_title = {
					limit = {
						is_noble_family_title = no
						tier >= tier_county
					}
					add_to_list = target_held_titles
				}
				capital_county = {
					save_temporary_scope_as = new_capital
				}
			}
			create_title_and_vassal_change = {
				type = usurped
				save_scope_as = change
				add_claim_on_loss = yes
			}
			scope:target.primary_title = {
				change_title_holder_include_vassals = {
					holder = root
					change = scope:change
				}
			}
			# We hide some of the effects, since they are all implicit
			hidden_effect = {
				every_in_list = {
					list = target_held_titles
					change_title_holder_include_vassals = {
						holder = root
						change = scope:change
					}
				}
				every_in_list = {
					list = target_vassals_list
					limit = {
						NOT = { this = root }
					}
					change_liege = {
						liege = root
						change = scope:change
					}
				}
				set_realm_capital = scope:new_capital
			}
			resolve_title_and_vassal_change = scope:change
		}
		change_influence_level = 1
		reverse_add_opinion = {
			target = scope:target
			modifier = deposed_me_opinion
		}

		stress_impact = {
			base = major_stress_impact_loss
		}
	}

	after = {
		create_character_memory = {
			type = deposed_liege_memory
			participants = { liege = scope:target }
		}
		trigger_event = grand_ambitions.0202
		if = {
			limit = {
				scope:story ?= {
					has_variable = hof_support
					var:hof_support = yes
				}
			}
			trigger_event = { # Coronation by the patriarch
				id = grand_ambitions.0201
				days = 2
			}
		}
		if = {
			limit = {
				scope:story ?= { has_variable = promised_foreign_ruler_claim }
			}
			trigger_event = {
				id = grand_ambitions.0300
				days = 5
			}
		}
	}
}

# Coronation by the HoF
grand_ambitions.0201 = {
	type = character_event
	title = grand_ambitions.0201.t
	desc = {
		desc = grand_ambitions.0201.desc
		triggered_desc = {
			trigger = {
				knows_language = language_greek
				religion = religion:christianity_religion
			}
			desc = grand_ambitions.0201.conc_greek
		}
	}
	theme = administrative

	left_portrait = {
		character = root
		triggered_animation = {
			trigger = { religion = religion:christianity_religion }
			animation = scepter # anim uses crucifix scepter
		}
		animation = war_over_tie
	}
	right_portrait = {
		character = root.faith.religious_head
		animation = obsequious_bow
	}

	immediate = { save_scope_as = root_scope }

	option = { 
		name = grand_ambitions.0201.a
		add_legitimacy = 1000
		add_piety = medium_piety_gain
		every_vassal = {
			limit = { has_vassal_stance = zealot }
			custom = every_zealot_vassal
			add_opinion = {
				target = root
				modifier = admiration_opinion
				opinion = 20
			}
		}
	}

	after = {
		scope:story ?= {
			if = {
				limit = {
					NOT = { has_variable = promised_foreign_ruler_claim }
				}
				end_story = yes
			}
		}
	}
}

# What to do with the deposed emperor?
grand_ambitions.0202 = {
	type = character_event
	title = grand_ambitions.0202.t
	desc = grand_ambitions.0202.desc
	theme = administrative
	override_background = {
		trigger = { scope:county_location = title:c_byzantion }
		reference = ep3_constantinople_riot
	}
	
	left_portrait = {
		character = root
		animation = schadenfreude
	}
	right_portrait = {
		character = scope:old_emp
		animation = beg
		override_imprisonment_visuals = yes
	}

	immediate = {
		# Saving these scopes for the follow-up (grand_ambitions.0003) for the old emp which references the lost title
		save_scope_as = actor
		save_scope_as = new_emp
		save_scope_as = root_scope # for loc
		primary_title = { save_scope_as = revoked }
		capital_county = { save_scope_as = county_location }

		random_owned_story = {
			limit = { story_type = grand_ambitions_story_cycle }
			save_scope_as = story
		}

		# save the proper scopes depending on how we got to this event
		switch = {
			trigger = scope:story.var:method
			flag:coup = {
				scope:defender = {
					save_scope_as = recipient
					save_scope_as = old_emp
				}
			}
			flag:scheme = {
				scope:target = {
					save_scope_as = recipient
					save_scope_as = old_emp
				}
			}
		}
		scope:target ?= {
			save_scope_as = recipient
			save_scope_as = old_emp
		}
		scope:old_emp = {
			add_character_flag = {
				flag = no_hat
				days = 5
			}
		}
	}

	option = { # Do not banish or de-nose the loser
		name = grand_ambitions.0202.a
		flavor = grand_ambitions.0202.a.flavor
		add_dread = minor_dread_loss
		change_influence = minor_influence_gain
		every_vassal = {
			limit = { has_vassal_stance = glory_hound }
			custom = every_glory_hound_vassal
			add_opinion = {
				target = root
				modifier = weak_opinion
				opinion = -10
			}
		}
		every_vassal = {
			limit = { has_vassal_stance = courtly }
			custom = every_courtly_vassal
			add_opinion = {
				target = root
				modifier = pleased_opinion
				opinion = 15
			}
		}
		if = {
			limit = {
				has_any_nickname = no
			}
			random = {
				chance = 10
				give_nickname = nick_the_merciful
			}
		}
		scope:target = {
			add_opinion = {
				target = root
				modifier = mercy_opinion
				opinion = 30
			}
			release_from_prison = yes
		}

		ai_chance = {
			base = 30
			modifier = {
				OR = {
					has_trait = patient
					has_trait = forgiving
					has_trait = humble
				}
				add = 50
			}
			ai_value_modifier = {
				ai_honor = 0.5
				ai_compassion = 0.75
			}
		}
	}

	option = { # Just de-nose the loser
		name = grand_ambitions.0202.b
		flavor = grand_ambitions.0202.b.flavor
		disfigure_recipient_effect = yes
		blind_castrate_and_disfigure_effect = yes #Stress & dread
		torture_blind_castrate_disfigure_opinion_effect = { VERB = disfigured }
		scope:target = { release_from_prison = yes }

		ai_chance = {
			base = 30
			modifier = {
				OR = {
					has_trait = sadistic
					has_trait = callous
				}
				add = 50
			}
			ai_value_modifier = {
				ai_boldness = 0.5
				ai_greed = 0.25
			}
		}
	}
	
	option = { # De-nose and Banish the scoundrel!
		name = grand_ambitions.0202.c
		flavor = grand_ambitions.0202.c.flavor

		disfigure_recipient_effect = yes
		blind_castrate_and_disfigure_effect = yes #Stress & dread
		torture_blind_castrate_disfigure_opinion_effect = { VERB = disfigured }

		scope:target = {
			banish_effect = { BANISHER = root }
			# Trigger Succession on the Noble Family Title of the prev emp
			create_title_and_vassal_change = {
				type = revoked
				save_scope_as = change
				add_claim_on_loss = yes
			}
			random_held_title = {
				limit = {
					has_title_law = noble_family_succession_law
				}
				change_title_holder = {
					holder = scope:target.primary_heir
					change = scope:change
				}
			}
			resolve_title_and_vassal_change = scope:change

			# Make the prev emp become a LAAMP
			show_as_tooltip = {
				create_landless_adventurer_title_effect = {
					REASON = flag:exile
					FLAVOR_CHAR = root
				}
			}
			if = {
				limit = { is_ai = no }
				trigger_event = grand_ambitions.0003
			}
			else = {
				trigger_event = grand_ambitions.0004
			}
			release_from_prison = yes
		}

		ai_chance = {
			base = 50
			modifier = {
				OR = {
					has_trait = vengeful
					has_trait = diligent
					has_trait = wrathful
					has_trait = paranoid
				}
				add = 60
			}
			ai_value_modifier = {
				ai_boldness = 1.5
				ai_zeal = 0.25
			}
		}
	}

	option = {
		name = grand_ambitions.0202.d
		custom_tooltip = grand_ambitions.0202.d.tt
	}

	after = {
		if = {
			limit = {
				scope:story ?= {
					NOR = {
						has_variable = hof_support
						has_variable = promised_foreign_ruler_claim
					}
				}
			}
			scope:story = { end_story = yes }
		}
	}
}

### FAILURE (0250-0290)
grand_ambitions.0250 = {
	type = character_event
	title = grand_ambitions.0250.t
	desc = grand_ambitions.0250.desc
	theme = administrative
	override_background = { reference = ep3_city_gate }

	left_portrait = {
		character = root
		animation = disappointed
	}
	right_portrait = {
		character = scope:interlocutor
		animation = shame
	}
	lower_center_portrait = scope:target

	immediate = {
		find_scheme_interlocutor_effect = yes
	}

	option = { # Acknowledge your failure
		name = grand_ambitions.0250.a
		scope:scheme = { end_scheme = yes }
		random_owned_story = {
			limit = { story_type = grand_ambitions_story_cycle }
			end_story = yes
		}

		stress_impact = {
			base = medium_stress_impact_gain
			ambitious = medium_stress_impact_gain
			arrogant = minor_stress_impact_gain
			diligent = minor_stress_impact_gain
			impatient = minor_stress_impact_gain
			stubborn = minor_stress_impact_gain
			lazy = minor_stress_impact_loss
			craven = minor_stress_impact_loss
			honest = minor_stress_impact_loss
		}
	}

	after = { scope:story = { end_story = yes } }
}


#######################################
###
###	FOLLOW-UP EVENTS
###
###	0300	Reap What You Sow
###
#######################################

# Reap What You Sow
# Event where the person you made a promise to comes to cash in
grand_ambitions.0300 = {
	type = character_event
	title = grand_ambitions.0300.t
	desc = grand_ambitions.0300.desc
	theme = emperor

	left_portrait = {
		character = root
		triggered_animation = {
			trigger = { religion = religion:christianity_religion }
			animation = acknowledging # anim uses crucifix scepter
		}
		animation = war_over_tie
	}
	right_portrait = {
		character = scope:foreign_ruler
		animation = personality_honorable
	}

	trigger = {
		any_owned_story = {
			story_type = grand_ambitions_story_cycle
			var:promised_foreign_ruler_claim ?= {
				is_ai = yes
				NOT = {
					any_held_title = {
						this = scope:story.var:foreign_ruler_demand
					}
				}
			}
		}
	}

	immediate = {
		scope:story.var:promised_foreign_ruler_claim = { save_scope_as = foreign_ruler }
		scope:story.var:foreign_ruler_demand ?= { save_scope_as = demand }
	}

	option = { # Grant them the title
		name = grand_ambitions.0300.a

		create_title_and_vassal_change = {
			type = granted
			save_scope_as = change
			add_claim_on_loss = yes
		}
		scope:demand = {
			change_title_holder_include_vassals = {
				holder = scope:foreign_ruler
				change = scope:change
			}
			holder = {
				every_held_title = {
					limit = {
						is_landless_type_title = no
						target_is_de_facto_liege_or_above = scope:demand
					}
					change_title_holder_include_vassals = {
						holder = scope:foreign_ruler
						change = scope:change
					}
				}
			}
		}
		resolve_title_and_vassal_change = scope:change

		add_legitimacy = major_legitimacy_loss 
	}

	option = { # Find a diplomatic out
		name = grand_ambitions.0300.b
		trigger = {
			NOT = {
				is_allied_to = scope:foreign_ruler
				is_at_war_with = scope:foreign_ruler
			}
		}
		duel = {
			skill = diplomacy
			target = scope:foreign_ruler 
			50 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
					min = -49
				}
				desc = grand_ambitions.0300.b.success
				send_interface_toast = {
					type = event_generic_good_text
					title =	grand_ambitions.0300.b.success
					left_icon = scope:foreign_ruler
					create_alliance = scope:foreign_ruler
				}
			}
			50 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3.5
					min = -49
				}
				desc = grand_ambitions.0300.b.failure
				send_interface_toast = {
					type = event_generic_bad_text
					title =	grand_ambitions.0300.b.failure
					left_icon = scope:foreign_ruler
					create_title_and_vassal_change = {
						type = granted
						save_scope_as = change
						add_claim_on_loss = yes
					}
					scope:demand = {
						change_title_holder_include_vassals = {
							holder = scope:foreign_ruler
							change = scope:change
						}
						holder = {
							every_held_title = {
								limit = {
									is_landless_type_title = no
									target_is_de_facto_liege_or_above = scope:demand
								}
								change_title_holder_include_vassals = {
									holder = scope:foreign_ruler
									change = scope:change
								}
							}
						}
					}
					resolve_title_and_vassal_change = scope:change
				}
			}
		}
	}
	
	option = { # Deny their claim
		name = grand_ambitions.0300.c
		add_legitimacy = minor_legitimacy_gain
		if = {
			limit = {
				NOT = {
					any_character_war = {
						OR = {
							primary_attacker = scope:foreign_ruler
							primary_defender = scope:foreign_ruler
						}
					}
				}
			}
			random_list = {
				1 = {
					show_chance = no
					scope:foreign_ruler = {
						start_war = {
							casus_belli = claim_cb
							target = root
							claimant = scope:foreign_ruler
							target_title = scope:demand
						}
					}
				}
				1 = { show_chance = no }
			}
		}
	}

	after = {
		scope:story ?= { end_story = yes }
	}
}


#######################################
###
###	MAINTENANCE EVENTS
###
###	0500	Invalid Story Target (dead or no longer holds target title)
###
#######################################

# While you're still scheming or claimant-ing, the target emperor dies or no longer holds the title
grand_ambitions.0500 = {
	type = letter_event
	opening = grand_ambitions.0500.opening
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:story.var:target_char ?= {
						is_alive = no
					}
				}
				desc = grand_ambitions.0500.dead
			}
			triggered_desc = {
				trigger = {
					scope:story.var:target_char ?= {
						NOT = { any_held_title = { this = scope:story.var:target_title } }
					}
				}
				desc = grand_ambitions.0500.wrong_title
			}
		}
	}
	sender = scope:interlocutor

	trigger = {
		any_owned_story = {
			story_type = grand_ambitions_story_cycle
			save_temporary_scope_as = temp_story
			exists = var:target_char 
			var:target_char = {
				NOT = { any_held_title = { this = scope:temp_story.var:target_title } }
			}
		}
		NOT = {
			any_held_title = {
				this = scope:story.var:target_title
			}
		}
	}

	immediate = {
		find_scheme_interlocutor_effect = yes
		random_owned_story = {
			limit = {
				story_type = grand_ambitions_story_cycle
			}
			save_scope_as = story
		}
	}

	option = { 
		name = grand_ambitions.0500.a
		trigger = { # Do you still meet the criteria for this story cycle?
			house = {
				house_head ?= root
				is_powerful_family = yes
			}
			is_independent_ruler = no
			highest_held_title_tier <= tier_kingdom
			government_allows = administrative
			top_liege = {
				primary_title.tier = tier_empire
				government_allows = administrative
			}
		}
		if = {
			limit = { scope:story.var:method ?= flag:scheme }
			custom_tooltip = {
				text = grand_ambitions_start_depose_scheme_tt
				# start le scheme
				## Balanced configuration.
				begin_scheme_with_agents_effect = {
					SCHEME_TYPE = depose
					TARGET_TYPE = target_character
					TARGET_SCOPE = root.top_liege
					# Success.
					AGENT_1 = agent_diplomat
					AGENT_2 = agent_tumbler
					# Speed.
					AGENT_3 = agent_infiltrator
					AGENT_4 = agent_socialite
					# Secrecy.
					AGENT_5 = agent_decoy
				}
				# Add a bussin scheme modifier
				random_scheme = {
					limit = { scheme_type = depose }
					add_scheme_modifier = {
						type = scheme_grand_ambitions_modifier
					}
				}
			}
		}
		# Update the story variables
		scope:story = {
			set_variable = {
				name = target_char
				value = root.top_liege
			}
			set_variable = {
				name = target_title
				value = root.top_liege.primary_title
			}
		}
	}

	option = {
		name = grand_ambitions.0500.b
		custom_tooltip = grand_ambitions.0500.b.tt
		scope:story = { end_story = yes }
		top_liege = {
			if = {
				limit = {
					scope:story.var:method ?= flag:coup
					any_targeting_faction = {
						faction_leader = root
						faction_is_type = claimant_faction
					}
				}
				random_targeting_faction = {
					limit = {
						faction_leader = root
						faction_is_type = claimant_faction
					}
					destroy_faction = yes
				}
			}
		}
	}
}
